package com.des1gn.config;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(NettyProperty.class)
public class NettyConfig {

    @Bean(name = "bossGroup",destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup bossGroup(NettyProperty nettyProperty){
        return new NioEventLoopGroup(nettyProperty.getBossThread());
    }

    @Bean(name = "workGroup",destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup workGroup(NettyProperty nettyProperty){
        return new NioEventLoopGroup(nettyProperty.getWorkThread());
    }

    @Bean(name = "serverBootStrap")
    public ServerBootstrap bootstrap(NettyProperty nettyProperty){
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup(nettyProperty),workGroup(nettyProperty)).channel(NioServerSocketChannel.class);
        serverBootstrap.option(ChannelOption.SO_KEEPALIVE,nettyProperty.getKeepAlive());
        serverBootstrap.option(ChannelOption.SO_BACKLOG, 1000);
        return serverBootstrap;
    }
}
